package edu.xalead;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest {
    public static void main(String[] args) {
        //mysql驱动类
        String driverClass = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://192.168.93.88:3306/mydb";
        String username = "root";
        String password = "";

        //数据库连接接口
        Connection conn = null;
        //执行sql语句的接口
        Statement st = null;
        try {
            //加载驱动类到虚拟机
            Class.forName(driverClass);

            //连接数据库
            conn = DriverManager.getConnection(url,username,password);

            //创建语句对象
            st = conn.createStatement();
            st.execute("Create database aaaaa;");
            String sql = "begin;";
            st.execute(sql);//
            sql = "update Account set account=account - 1000 where id=1; ";
            st.execute(sql);
            sql = "update Account set account=account + 1000 where id=2;";
            st.execute(sql);
            sql = "commit;";
            st.execute(sql);
        } catch (Exception e) {
            String sql = "rollback;";
            try {
                st.execute(sql);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            try {
                if(st != null)
                    st.close();
                if(conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
